﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ICSharpCode.WinFormsUI.Controls
{
    public partial class TextBoxControl : TextBoxBase
    {
        private TextBoxBase textBox = null;
        private string _text = string.Empty;
        private int _lastDocumentType = -1;

        public override string Text
        {
            get
            {
                return textBox == null ? _text : textBox.Text;
            }
            set
            {
                _text = value;
                if (textBox != null)
                    textBox.Text = _text;
            }
        }

        public override DocumentType DocumentType
        {
            get
            {
                return base.DocumentType;
            }
            set
            {
                if (this.textBox != null
                    && this.textBox.Text != "undefined")
                {
                    this._text = this.textBox.Text;
                }
                base.DocumentType = value;
                InitializeControl();
            }
        }

        public TextBoxControl()
        {
            InitializeComponent();             
        }

        protected void InitializeControl()
        {
            int _newDocumentType = 0;

            switch (documentType)
            {
                case WinFormsUI.Controls.DocumentType.None:
                case WinFormsUI.Controls.DocumentType.Text:
                case WinFormsUI.Controls.DocumentType.Word:                   
                    _newDocumentType = 1;
                    break;
                default:                     
                    _newDocumentType = 0;
                    break;
            }

            if (_newDocumentType != _lastDocumentType)
            {
                this.BackColor = Color.White;
                this.Controls.Clear();
                switch (documentType)
                {
                    case WinFormsUI.Controls.DocumentType.None:
                    case WinFormsUI.Controls.DocumentType.Text:
                    case WinFormsUI.Controls.DocumentType.Word:
                        textBox = new DocoFormatControl();
                        break;
                    default:
                        textBox = new CodeFormatControl();
                        break;
                }

                textBox.Dock = DockStyle.Fill;
                this.Controls.Add(textBox);
            }            

            textBox.DocumentType = documentType;
            textBox.Text = _text;
           
            _lastDocumentType = _newDocumentType;

        }

        public override void ReleaseDispose()
        {
            textBox.ReleaseDispose();
        }

    }
}
